//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Date.h"
using namespace Cephei::QL::Times;
#include <gen/QL/Times/Period.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::CDate::CDate (Int64 serialNumber) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDate = NULL;
#endif
        QuantLib::BigInteger _serialNumber = (QuantLib::BigInteger)ValueHelper::Convert (serialNumber);
        _ppDate = new boost::shared_ptr<QuantLib::Date> (new QuantLib::Date ( _serialNumber ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CDate::CDate () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDate = NULL;
#endif
        _ppDate = new boost::shared_ptr<QuantLib::Date> (new QuantLib::Date ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CDate::CDate (boost::shared_ptr<QuantLib::Date>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDate = NULL;
#endif
	_ppDate = &childNative;
    
}
Cephei::QL::Times::CDate::CDate (QuantLib::Date& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDate = NULL;
#endif
	_ppDate = new boost::shared_ptr<QuantLib::Date> (&childNative);
    
    _DateOwner = owner;
    
}

Cephei::QL::Times::CDate::CDate (CDate^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDate = new boost::shared_ptr<QuantLib::Date> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CDate::CDate (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDate = NULL;
#endif
	if (!t->IsSubclassOf(CDate::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CDate::CDate (QuantLib::Handle<QuantLib::Date>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDate = &childNative;
	_ppDate = &static_cast<boost::shared_ptr<QuantLib::Date>>(childNative.currentLink());
    
    _DateOwner = owner;
}
Cephei::QL::Times::CDate::CDate (QuantLib::Handle<QuantLib::Date> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDate = &childNative;
	_ppDate = &static_cast<boost::shared_ptr<QuantLib::Date>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CDate::CDate (QuantLib::Date childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDate = NULL;
#endif
	_ppDate = new boost::shared_ptr<QuantLib::Date> (new QuantLib::Date (childNative));
    
}
#endif

Cephei::QL::Times::CDate::~CDate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDate != NULL)
    {
	    delete _ppDate;
        _ppDate = NULL;
    }
}
Cephei::QL::Times::CDate::!CDate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDate != NULL)
    {
	    delete _ppDate;
    }
}
QuantLib::Date& Cephei::QL::Times::CDate::GetReference ()
{
    if (_ppDate == NULL) throw gcnew NativeNullException ();
	return **_ppDate;
}
boost::shared_ptr<QuantLib::Date>& Cephei::QL::Times::CDate::GetShared ()
{
    if (_ppDate == NULL) throw gcnew NativeNullException ();
	return *_ppDate;
}
QuantLib::Date* Cephei::QL::Times::CDate::GetPointer ()
{
    if (_ppDate == NULL) throw gcnew NativeNullException ();
	return &**_ppDate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Date>& Cephei::QL::Times::CDate::GetHandle ()
{
	if (_phDate == NULL)
	{
		_phDate = new Handle<QuantLib::Date> (*_ppDate);
	}
	return *_phDate;
}
#endif
bool Cephei::QL::Times::CDate::HasNative () 
{
	return (_ppDate != NULL);
}

DateTime Cephei::QL::Times::CDate::EndOfMonth (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDate)->endOfMonth ( _d );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CDate::IsEndOfMonth (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	bool _rv = (bool)(*_ppDate)->isEndOfMonth ( _d );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CDate::MaxDate::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDate)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CDate::MinDate::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDate)->minDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::MonthEnum Cephei::QL::Times::CDate::Month::get ()
{
    try
    {
    	QuantLib::Month _rv = (QuantLib::Month)(*_ppDate)->month ( );   
        QL::Times::MonthEnum _nrv = (QL::Times::MonthEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CDate::NextWeekday (DateTime d, QL::Times::WeekdayEnum w)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        QuantLib::Weekday _w = (QuantLib::Weekday)w ;
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDate)->nextWeekday ( _d,  _w );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int64 Cephei::QL::Times::CDate::SerialNumber::get ()
{
    try
    {
    	QuantLib::BigInteger _rv = (QuantLib::BigInteger)(*_ppDate)->serialNumber ( );   
        Int64 _nrv = (Int64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CDate::TodaysDate::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDate)->todaysDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::WeekdayEnum Cephei::QL::Times::CDate::Weekday::get ()
{
    try
    {
    	QuantLib::Weekday _rv = (QuantLib::Weekday)(*_ppDate)->weekday ( );   
        QL::Times::WeekdayEnum _nrv = (QL::Times::WeekdayEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::IDate^ Cephei::QL::Times::CDate_Factory::Create (Int64 serialNumber)
{
    return gcnew CDate ( serialNumber);
}
Cephei::QL::Times::IDate^ Cephei::QL::Times::CDate_Factory::Create ()
{
    return gcnew CDate ();
}
